import IMAGE_SINA from "./asset/images/icon/sina.png";
import IMAGE_DOUBAN from "./asset/images/icon/douban.png";
import IMAGE_WECHAT from "./asset/images/icon/wechat.png";
import IMAGE_DOUYIN from "./asset/images/icon/douyin.png";
import IMAGE_TOUTIAO from "./asset/images/icon/toutiao.png";
import IMAGE_ZHIHU from "./asset/images/icon/zhihu.png";
import IMAGE_TXVIDEO from "./asset/images/icon/txvideo.png";
import IMAGE_AIQIYI from "./asset/images/icon/aiqiyi.png";
import IMAGE_YOUKU from "./asset/images/icon/youku.png";
import IMAGE_BILIBILI from "./asset/images/icon/bilibili.png";
import IMAGE_MGTV from "./asset/images/icon/mgtv.png";
import IMAGE_MAOYAN from "./asset/images/icon/maoyan.png";
import IMAGE_TPP from "./asset/images/icon/tpp.png";
import IMAGE_TXNEW from "./asset/images/icon/txnew.png";
import IMAGE_WYNEW from "./asset/images/icon/wynew.png";
import IMAGE_XHS from "./asset/images/icon/xhs.png";
import IMAGE_QQMUSIC from "./asset/images/icon/qqmusic.png";
import IMAGE_QQ from "./asset/images/icon/qq.png";
import IMAGE_HPTY from "./asset/images/icon/hpty.png";
import IMAGE_KS from "./asset/images/icon/ks.png";
import IMAGE_TU from "./asset/images/icon/tu.png";
import IMAGE_TIEBA from "./asset/images/icon/tieba.png";
import IMAGE_SINANEW from "./asset/images/icon/sinanew.png";
import IMAGE_ORDER from "./asset/images/icon/order.png";
import * as utils from "./utils";

export const platform = {
  order: {
    image: IMAGE_ORDER,
  },
  0: {
    image: IMAGE_SINA,
    name: "龙眼",
  },
  1: {
    image: IMAGE_DOUBAN,
    name: "绿豆",
  },
  2: {
    image: IMAGE_WECHAT,
    name: "月亮",
  },
  3: {
    image: IMAGE_DOUYIN,
    name: "符号",
  },
  4: {
    image: IMAGE_TOUTIAO,
    name: "笔记本",
  },
  5: {
    image: IMAGE_ZHIHU,
    name: "北极狐",
  },
  6: {
    image: IMAGE_TXVIDEO,
    name: "三角形",
  },
  7: {
    image: IMAGE_AIQIYI,
    name: "手办",
  },
  8: {
    image: IMAGE_YOUKU,
    name: "右箭头",
  },
  9: {
    image: IMAGE_BILIBILI,
    name: "电视机",
  },
  10: {
    image: IMAGE_MGTV,
    name: "芒果",
  },
  11: {
    image: IMAGE_MAOYAN,
    name: "眼睛",
  },
  12: {
    image: IMAGE_TPP,
    name: "电影票",
  },
  13: {
    image: IMAGE_TXNEW,
    name: "企鹅书本",
  },
  14: {
    image: IMAGE_WYNEW,
    name: "蚊香书本",
  },
  15: {
    image: IMAGE_XHS,
    name: "书本",
  },
  16: {
    image: IMAGE_QQMUSIC,
    name: "绿帽",
  },
  17: {
    image: IMAGE_QQ,
    name: "企鹅",
  },
  18: {
    image: IMAGE_HPTY,
    name: "老虎",
  },
  19: {
    image: IMAGE_KS,
    name: "相机",
  },
  20: {
    image: IMAGE_TU,
    name: "兔子",
  },
  21: {
    image: IMAGE_TIEBA,
    name: "手印",
  },
  22: {
    image: IMAGE_TIEBA,
    name: "蚊香",
  }
}

export const formatTaskData = (data) => {
  if (data.length === 0) {
    return [];
  }
  const result = [];
  let time = getTime(data[0].apply_time);
  let temp = setTaskObj(time, data[0]);
  result.push(temp);
  let tempTime;
  for (let i = 1, len = data.length; i < len; i++) {
    tempTime = getTime(data[i].apply_time);
    if (tempTime === time) {
      temp.count++;
      temp.data.push(data[i]);
    } else {
      time = tempTime;
      temp = setTaskObj(time, data[i]);
      result.push(temp);
    }
  }
  return result;
}

export const concatTaskData = (old, now) => {
  if (old.length === 0) {
    return now;
  }
  const oldLen = old.length;
  const first = now.shift();
  const last = old[oldLen - 1];
  if (last.time === first.time) {
    last.count += first.count;
    last.data = [...last.data, ...first.data];
  }
  return [...old, first, ...now];
}

function setTaskObj(time, data) {
  return {
    time,
    count: 1,
    data: [data],
  };
}

function getTime(unix) {
  return utils.formatUnix({
    unix,
    fmt: 'Y-M-D',
  });
}